#pragma once


#include "common.h"
#include "laplace.h"


using namespace std;



/**
 * @brief 将经纬度坐标转化为笛卡尔平面坐标,假设地球是正圆,因为找到了地球坐标转换库proj，该函数弃用
 * 
 * @param lon 经度
 * @param lat 纬度 
 * @param x 
 * @param y 
 */
inline void geoToCartesian(double lat, double lon,  double& x, double& y) {
    cout<<"转换前:"<<lat<<"\t"<<lon<<endl;
    lat = (lat * PI) / 180.0;
    lon = (lon * PI) / 180.0;
    x = EARCH_R * cos(lat) * cos(lon);
    y = EARCH_R * cos(lat) * sin(lon);
    // z = EARCH_R * sin(lat);
    cout<<"转换后:"<<x<<"\t"<<y<<endl;
}

//读取文件内容
void read(const string& fileName,int& workerNum,int& taskNum,int& sumNum){
    ifstream fin(fileName.c_str(),ios::in);
    // cout<<fileName<<endl;
    // 检查文件路径是否正确
    if (!fin.is_open())
    {
        cout<<"Error openning file:"<<"\""<<fileName<<"\""<<endl;
        exit(1);
    }
    
    int Umax;
    double beginTime,endTime,longitude,latitude,radius;
    string type;
    Node node;
    //读取初始量
    fin>>workerNum>>taskNum>>Umax>>sumNum;

    //读取数据
    for (int i = 0; i < sumNum; i++)
    {
        fin>>beginTime>>type;
        if (type[0]=='w')
        {
            node.type=worker;
            fin>>longitude>>latitude>>radius>>node.capacity>>endTime>>node.cost.rate;//习惯性纬度，经度顺序
            // geoToCartesian(latitude,longitude,node.loc.first,node.loc.second);
            node.loc.first = latitude;
            node.loc.second = longitude;
            endTime+=beginTime;
            node.beginTime=beginTime;
            node.endTime=endTime;
            node.radius=radius;//转化为米
            workerLists.push_back(node);
            // cout<<beginTime<<"\t"<<x<<"\t"<<y<<"\t"<<radiu<<"\t"<<cap<<"\t"<<endTime+beginTime<<"\t"<<rate<<endl;
            // node.show();
            // cout<<endl;
            cout<<"a1:"<<node.loc.first<<"\t"<<node.loc.second<<endl;
            laplaceForGeo_In(l,r,node.loc);
            perturbWorkers.push_back(node);
            cout<<"a2:"<<node.loc.first<<"\t"<<node.loc.second<<endl;
        }else{
            node.type=task;
            fin>>longitude>>latitude>>endTime>>node.cost.pay;
            // geoToCartesian(latitude,longitude,node.loc.first,node.loc.second);
            node.loc.first = latitude;
            node.loc.second = longitude;
            endTime+=beginTime;
            node.beginTime=beginTime;
            node.endTime=endTime;
            taskLists.push_back(node);
            // node.show();
            // cout<<beginTime<<"\t"<<x<<"\t"<<y<<"\t"<<endTime+beginTime<<"\t"<<pay<<endl;
            laplaceForGeo_In(l,r,node.loc);
            perturbTasks.push_back(node);
        }
        
    }
    

}

void read2(const string& fileName,int& workerNum,int& taskNum,int& sumNum){
    ifstream fin(fileName.c_str(),ios::in);
    // cout<<fileName<<endl;
    // 检查文件路径是否正确
    if (!fin.is_open())
    {
        cout<<"Error openning file:"<<"\""<<fileName<<"\""<<endl;
        exit(1);
    }

    double beginTime=0,endTime=10,longitude,latitude,radius;
    string type;
    Node node;
    //读取初始量
    fin>>workerNum>>taskNum>>sumNum;

    //读取数据
    for (int i = 0; i < sumNum; i++)
    {

        fin>>type;
        if (type[0]=='w')
        {
            node.type=worker;
            fin>>longitude>>latitude>>radius;//习惯性经纬度
            // geoToCartesian(latitude,longitude,node.loc.first,node.loc.second);
            node.loc.first = longitude;
            node.loc.second = latitude;
            endTime+=beginTime;
            node.beginTime=beginTime;
            node.endTime=endTime;
            node.radius=radius;
            workerLists.push_back(node);

            // cout<<"a1:"<<node.loc.first<<"\t"<<node.loc.second<<endl;
            // printf("a1:%.6lf \t %.6lf\n",node.loc.first,node.loc.second);
            laplaceForGeo_In(l,r,node.loc);
            perturbWorkers.push_back(node);
            // cout<<"a2:"<<node.loc.first<<"\t"<<node.loc.second<<endl;
            // printf("a2:%.6lf \t %.6lf\n",node.loc.first,node.loc.second);
            Xposition.push_back(node.loc);            
        }else{
            node.type=task;
            fin>>longitude>>latitude;
            // geoToCartesian(latitude,longitude,node.loc.first,node.loc.second);
            node.loc.first = longitude;
            node.loc.second = latitude;
            endTime+=beginTime;
            node.beginTime=beginTime;
            node.endTime=endTime;
            taskLists.push_back(node);
            // node.show();
            // cout<<beginTime<<"\t"<<x<<"\t"<<y<<"\t"<<endTime+beginTime<<"\t"<<pay<<endl;
            laplaceForGeo_In(l,r,node.loc);
            perturbTasks.push_back(node);
            Xposition.push_back(node.loc);
        }
        
    }
    calSMatrix();//计算相关矩阵
}

//地理坐标转换
int convertToCartesian(){
    // PJ_CONTEXT *C;
    // PJ *P;
    // PJ *norm;
    // PJ_COORD a, b;
    // C = proj_context_create();

    // P = proj_create_crs_to_crs (C,
    //                             "EPSG:4326",//GPS所用坐标系,EPSG:4326
    //                             "EPSG:4087", /* or EPSG:32632 */
    //                             NULL);

    // if (0 == P) {
    //     fprintf(stderr, "Failed to create transformation object.\n");
    //     return 1;
    // }
    // norm = proj_normalize_for_visualization(C, P);
    // if (0 == norm) {
    //     fprintf(stderr, "Failed to normalize transformation object.\n");
    //     return 1;
    // }
    // proj_destroy(P);
    // P = norm;
    // a = proj_coord(0.460907,2.421540, 0, 0);
    // b = proj_trans(P, PJ_FWD, a);

    // proj_destroy(P);
    // proj_context_destroy(C); 
    return 0;
}
